#!/bin/bash

_help() {
    echo "zsh   -- install zsh"
    echo "znap  -- install znap"
    echo "tools -- install tools eg: fd,rg,lsd"

}

run_first() {
    if [ ! -e /usr/local/bin/d ]; then

        ln -sv "$PWD"/main.sh /usr/local/bin/d
        ln -sv "$PWD"/.zshenv ~
        echo 使用 d 命令帮助
    fi
}

# [ ! -e $HOME/bin ] && mkdir $HOME/bin

install_zsh() {
    zsh_url='https://zbluo-file.s3.bitiful.net/zsh.tgz'
    wget -nc $zsh_url -P /opt
    if [ ! -d /opt/zsh ]; then
        tar xf /opt/zsh.tgz -C /opt
        ln -sv /opt/zsh/bin/zsh /usr/bin/zsh
        [ $(grep -c zsh /etc/shells) -eq 0 ] && echo '/opt/zsh/bin/zsh' >>/etc/shells

        usermod -s /opt/zsh/bin/zsh $USER
    else
        echo $(which zsh) existed
    fi
}

install_znap() {
    znap_url='https://zbluo-file.s3.bitiful.net/znap.tgz'
    [ ! -e $HOME/.local/share ] && mkdir -p $HOME/.local/share
    wget -nc $znap_url -P /opt
    tar xf /opt/znap.tgz -C $HOME/.local/share
}

install_tools() {
    wget -nc "https://zbluo-file.s3.bitiful.net/tools.tar.gz" -P /opt
    [ ! -e $HOME/bin ] && tar xf /opt/tools.tar.gz -C $HOME || echo $HONE/bin existed
}

run_first

case $1 in
'')
    _help
    ;;
zsh)
    install_zsh
    ;;
znap)
    install_znap
    ;;
tools)
    install_tools
    ;;
esac
